package com.system.demo.service.impl;

import com.system.demo.doMain.entity.User;
import com.system.demo.mapper.UserMapper;
import com.system.demo.service.UserService;
import com.system.demo.utils.CommonUtils;
import com.system.demo.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findUser(String name,String password) {

        User user= userMapper.findUser(name,password);
        if(user!=null){
            return user;
        }else{
            return null;
        }
    }

    @Override
    public List<User> list() {
        return userMapper.list();
    }

    @Override
    public int save(Map<String, String> userInfo) {
        User user=parseToUser(userInfo);
        if(user!=null){
            return userMapper.save(user);
        }
        return -1;
    }

    @Override
    public User findUserByName(String name) {
        return userMapper.findUserByName(name);
    }

    private User parseToUser(Map<String, String> userInfo) {
        if(userInfo.containsKey("name")&&userInfo.containsKey("password")){
            User user=new User();
            user.setName(userInfo.get("name"));
            String password=userInfo.get("password");
            user.setPassword(CommonUtils.MD5(password));
            return user;
        }
        return null;
    }
}
